package kassiopeia.commons.model.json;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
import kassiopeia.commons.model.plugin.PluginDescriptor;

public final class PluginDescriptorJsonSerializer {

    private static final JsonConfig jsonConfig;
    private static final String[] IGNORED_FIELDS = {"id"};

    static {
        jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(IGNORED_FIELDS);
        jsonConfig.setJsonPropertyFilter(new JsonPropertyFilter());
    }

    private PluginDescriptorJsonSerializer() {}

    public static String toJson(PluginDescriptor pluginDescriptor) {
        return JSONObject.fromObject(pluginDescriptor, jsonConfig).toString(4);
    }


    private static final class JsonPropertyFilter implements PropertyFilter {
        @Override
        public boolean apply(Object source, String name, Object value) {
            if((value == null) || ("".equals(value))) { return true; }
            return false;
        }
    }
}
